<?php

namespace App\Store\Controllers\Sysset;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Store\Models\Sysset\GoodsSendpoints;
use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Account\SiteStore;
use App\Store\Models\Basic\GoodsCategory;
use App\Store\Validate\Sysset\GoodsSendpoints as ValidateBasicGoodsSendpoints;

/**
 * 商品积分信息
 */
class GoodsSendpointsController extends Controller
{
    /**
     * 获取列表
     */
    public function getlist(Request $request)
    {
        $list = GoodsSendpoints::getList($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    /**
     * 获取详情
     */
    public function getinfo(Request $request)
    {
        $id = $request->id ?? 0;
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $info = GoodsSendpoints::where(['id' => $id, 'cp_id' => $cp_id, 'is_del' => 1])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => ['info' => $info]]);
    }

    /**
     * 新增
     */
    public function dopostadd(Request $request)
    {   

        $req_data = $request->only(['store_id','goods_category_ids','type','points_num','share_mem_points_scale','odds','s_time','e_time','remark']);


        $Validate = new ValidateBasicGoodsSendpoints();
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }
        $accountinfo= StoreCommon::getCurrentAccountinfo($request);

        $ins_data=[];
        $store_id=$req_data['store_id'];
        $storeinfo = SiteStore::where(['id' => $store_id, 'cp_id' => $accountinfo['cp_id']])->first();
        if (!$storeinfo) {
            return response()->json(['code' => '001', 'msg' => '无效门店参数']);
        }
        if($req_data['type']==2){
            $ins_data[]=[
                'cp_id'=> $accountinfo['cp_id'],
                'store_id'=> $store_id,
                'goods_category_id'=>0,
                'type'=>$req_data['type'],
                'points_num'=>$req_data['points_num'],
                'share_mem_points_scale'=>$req_data['share_mem_points_scale'],
                'odds'=>isset($req_data['odds'])?$req_data['odds']:0,
                's_time'=>isset($req_data['s_time'])?strtotime($req_data['s_time']):0,
                'e_time'=>isset($req_data['e_time'])?strtotime($req_data['e_time']):0,
                'remark'=>$req_data['remark'],
                'create_at'=>time(),
                'update_at'=>time(),
                'create_account_id' => $accountinfo['id'],
                'update_account_id' => $accountinfo['id'],
                'create_account_real_name' => $accountinfo['real_name'],
                'update_account_real_name' => $accountinfo['real_name'],
            ];
        }else{

            if(!isset($req_data['goods_category_ids']) || !$req_data['goods_category_ids']){
                return response()->json(['code' => '001', 'msg' => '无效商品类别参数']);
            }
            $goods_category_ids = $req_data['goods_category_ids'];
            $goods_category_ids = is_array($goods_category_ids) ? $goods_category_ids : (is_array(json_decode($goods_category_ids, true)) ? json_decode($goods_category_ids, true) : []);
            if (count($goods_category_ids) <= 0) {
    
                return response()->json(['code' => '001', 'msg' => '无效商品类别参数']);
            }
            foreach($goods_category_ids as $goods_category_id){
                $categoryinfo = GoodsCategory::where(['id' => $goods_category_id,'store_id'=>$store_id, 'cp_id' => $accountinfo['cp_id']])->first();
                if (!$categoryinfo) {
                    return response()->json(['code' => '001', 'msg' => '无效商品类别参数']);
                }
                $isexist=GoodsSendpoints::where([['store_id', $store_id], ['goods_category_id',$goods_category_id]])->first();
                if($isexist){
                    return response()->json(['code' => '001', 'msg' => '门店商品类别积分配置不允许重复']);
                }
    
                $ins_data[]=[
                    'cp_id'=> $accountinfo['cp_id'],
                    'store_id'=> $store_id,
                    'goods_category_id'=>$goods_category_id,
                    'type'=>$req_data['type'],
                    'points_num'=>$req_data['points_num'],
                    'share_mem_points_scale'=>$req_data['share_mem_points_scale'],
                    'odds'=>isset($req_data['odds'])?$req_data['odds']:0,
                    's_time'=>isset($req_data['s_time'])?strtotime($req_data['s_time']):0,
                    'e_time'=>isset($req_data['e_time'])?strtotime($req_data['e_time']):0,
                    'remark'=>$req_data['remark'],
                    'create_at'=>time(),
                    'update_at'=>time(),
                    'create_account_id' => $accountinfo['id'],
                    'update_account_id' => $accountinfo['id'],
                    'create_account_real_name' => $accountinfo['real_name'],
                    'update_account_real_name' => $accountinfo['real_name'],
                ];
    
            }
        }
        GoodsSendpoints::insert($ins_data);
        return response()->json(['code' => '000', 'msg' => '新增成功']);
    }

    /**
     * 编辑
     */
    public function dopostedit(Request $request)
    {
        $req_data = $request->only(['id', 'points_num','share_mem_points_scale','odds','s_time','e_time','remark']);

        $Validate = new ValidateBasicGoodsSendpoints('edit');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }
        $accountinfo= StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];
        
        $info = GoodsSendpoints::where([['id', $req_data['id']], ['cp_id', $cp_id], ['is_del', 1]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }

        $data = $req_data;
        unset($data['id']);
        $data['update_at'] = time();
        $data['update_account_id'] = $accountinfo['id'];
        $data['update_account_real_name'] = $accountinfo['real_name'];
        GoodsSendpoints::where(['id' => $req_data['id']])->update($data);
        return response()->json(['code' => '000', 'msg' => '编辑成功']);
    }
    /**
     * 删除
     */
    public function dodel(Request $request)
    {
        $id = $request->id ?? 0;
        $cp_id = StoreCommon::getCurrentAccountCompanyId($request);
        $info = GoodsSendpoints::where([['id', $id], ['cp_id', $cp_id], ['is_del', 1]])->first();
        if (!$info) {
            return response()->json(['code' => '001', 'msg' => '参数错误']);
        }
        GoodsSendpoints::where([['id', $id]])->update(['is_del' => 2, 'del_at' => time()]);

        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }
}
